<template>
  <div>
    <div>
      <el-form :inline="true" :model="form">
        <el-button-group>
          <el-button type="primary" icon="edit" @click="editHandler"/>
          <el-input v-model="form.fileName" clearable placeholder="企业名称" style="width:200px;"/>
          <el-form-item>
            <el-button type="primary" @click="_queryPictureByFileName">搜索</el-button>
          </el-form-item>
        </el-button-group>
      </el-form>
    </div>

    <el-table :data="getSysFiles" height="300" border :stripe="true" :highlight-current-row="true"
              @current-change="tableRowChangeClickHandler"
              style="width: 100%">
      <el-table-column prop="profileId" label="企业编号" min-width="20%"/>
      <el-table-column prop="profileName" label="企业名称" min-width="20%"/>
      <el-table-column prop="organAgentName" label="经办人姓名" min-width="20%"/>
      <el-table-column prop="organAgentIdcard" label="经办人身份证" min-width="20%"/>
      <el-table-column prop="organAgentMobile" label="经办人手机号" min-width="20%"/>
    </el-table>

    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="getSysFileCurrentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="getFilesCount">
      </el-pagination>
    </div>

    <upload-form ref="uploadForm" :mode="formMode" :param="currentRecord"/>

    <button v-clipboard="copyData" @success="copySucHandler" @error="copyErrorHandler"
            ref="btnCopy" style="display:none">copy
    </button>


  </div>
</template>

<script type="text/babel">
  import {mapActions} from 'vuex'

  import operatorStore from './bll/operatorStore'
  import store from '../../store'

  (!store.state.operatorStore) && store.registerModule('operatorStore', operatorStore)


  import UploadForm from './uploadForm.vue';


  export default {
    data() {
      return {
        form: {
          fileName: ''
        },
        formMode: null,
        currentRecord: null,
        pageSize: 10,
        pageIndex: 1,
        //form: {},
        copyData: '',
        qrData: '',
        privFileUrlFlag: false
      }
    },
    components: {UploadForm},
    computed: {
      getSysFiles() {
        return this.$store.state.operatorStore.files;
      },
      getFilesCount() {
        return this.$store.state.operatorStore.totalCount;
      },
      getSysFileCurrentPage() {
        return this.$store.state.operatorStore.currentPage;
      }
    },

    mounted() {
      console.log('file upload page mounted.');
      this.initPage();
    },

    methods: {
      ...mapActions(['searchOperatorCount', 'searchOperator', 'queryOperator', 'editFile']),

      initPage() {
        this.queryOperator();
      },

      openUploadDialogHandler() {
        this.$refs.uploadDialog.open();
      },

      editHandler() {
        if (!this.currentRecord) {
          this.$message.warning('请选择一条记录');
          return;
        }

        this.$refs.uploadForm.enable();
        this.formMode = 'EDIT';
      },
      copySucHandler(e) {
        this.$message.info('链接拷贝成功');
        this.privFileUrlFlag = false;
      },
      copyErrorHandler(e) {
        console.error('fail to copy url', e);
        let msg = '拷贝失败，请手动复制地址';
        if (this.privFileUrlFlag) {
          msg = '拷贝失败，请再尝试一次';
        }
        this.$message.warning(msg);
      },

      tableRowChangeClickHandler(currentRow, oldCurrentRow) {
        console.log(currentRow);
        this.currentRecord = currentRow;
        this.$refs.uploadForm.disable();
      },

      handleSizeChange(val) {
        console.log(`每页 ${val} 条`)
        this.pageSize = val
        var profileName = this.form.fileName
        if (this.form.fileName !== '') {
          this.queryOperator({profileName: profileName, pageIndex: 1, pageSize: val})
        } else {
          this.queryOperator({pageIndex: 1, pageSize: val})
        }
      },
      handleCurrentChange(val) {
        console.log(arguments)
        console.log(`current${val}`)
        this.pageIndex = val
        var profileName = this.form.fileName
        if (this.form.fileName !== '') {
          this.searchOperator({profileName: profileName, pageIndex: val, pageSize: this.pageSize})
        } else {
          this.queryOperator({pageIndex: val, pageSize: this.pageSize})
        }
      },
      formatterHandler(row, column) {
        switch (column.property) {
          case 'state':
            return row.state === 1 ? 'Yes' : 'Discard';
        }
      },
      _queryPictureByFileName() {
        console.log(this.form.fileName)
        var profileName = this.form.fileName
        if (this.form.fileName !== '') {
          this.searchOperator({profileName: profileName})
          this.searchOperatorCount({profileName: profileName})
        } else {
          this.queryOperator()
        }
      }
    }
  }
</script>

<style lang="scss">
  .download-url {
    padding: 5px;
  }

  .qrcode-wrapper {
    margin: auto;
  }
</style>

